#include "TCPTaskPool.h"
#include "GatewayTaskManager.h"
#include "GatewayTimeTick.h"
#include "GatewayServer.h"

void GatewayServer::newTCPTask(int connfd, struct sockaddr_in * client )
{
	GatewayTask * task = new GatewayTask(connfd, client);
	if ( NULL == task )
		TEMP_FAILURE_RETRY(::close(connfd));
	else if ( !pool->addVerify(task))
	{	// ::close will be called in destructor
		delete task;
	}
}

bool GatewayServer::init()
{
	GatewayTaskManager::creatInstance();
	GatewayTimeTick::creatInstance();

	pool = new TCPTaskPool(256);// create thread pool
	if ( pool == NULL || !pool->init())
	{
		return false;
	}

	if ( !TCPClientServer::init())
		return false;
	if ( !TCPServer::bind(this->port))
		return false;

	return GatewayTimeTick::getInstance()->start();
}

bool GatewayServer::sendLoginCmd()
{
	RequestLoginCmd cmd;
	return this->superClient.sendPacket(&cmd, sizeof(RequestLoginCmd));
}

int main()
{
	GatewayServer::creatInstance();
	GatewayServer::getInstance()->mainLoop();

	return 0;
}

